package com.a16.yuejian.yuejian.model;

import com.a16.yuejian.yuejian.javabean.Activity;
import com.a16.yuejian.yuejian.javabean.Collected;
import com.a16.yuejian.yuejian.javabean.Order;
import com.a16.yuejian.yuejian.javabean.Success;
import com.a16.yuejian.yuejian.listener.ActivityListener;
import com.a16.yuejian.yuejian.listener.CollectedListener;
import com.a16.yuejian.yuejian.listener.Listener;
import com.a16.yuejian.yuejian.listener.OrderListener;
import com.a16.yuejian.yuejian.listener.SuccessListener;
import com.a16.yuejian.yuejian.service.ActivityService;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by 55080 on 2017/9/30.
 */

public class ActivityModel {
    private Retrofit retrofit;
    public ActivityModel(){
        retrofit =new Retrofit.Builder()
                .baseUrl("http://10.0.2.2:8080/neu_yuejian201/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
    public void allactivity(final Listener listener){
        ActivityService activityService = retrofit.create(ActivityService.class);
        Call call = activityService.allactivity();
        Callback<List<Activity>> callback=new Callback<List<Activity>>() {
            @Override
            public void onResponse(Call<List<Activity>> call, Response<List<Activity>> response) {
                if (response.isSuccessful() && response.body()!=null)
                {
                    listener.onResponse(response.body());
                }
            }

            @Override
            public void onFailure(Call<List<Activity>> call, Throwable t) {

            }
        };
        call.enqueue(callback);
    }

    public void getactivity(String ac_id,final ActivityListener listener){
        ActivityService activityService = retrofit.create(ActivityService.class);
        Call call = activityService.getactivity(ac_id);
        Callback<Activity> callback = new Callback<Activity>() {
            @Override
            public void onResponse(Call<Activity> call, Response<Activity> response) {
                if (response.isSuccessful() && response.body()!=null)
                {
                    listener.onResponse(response.body());
                }
            }

            @Override
            public void onFailure(Call<Activity> call, Throwable t) {

            }
        };
        call.enqueue(callback);
    }

    public void follow(String ac_id, String uid, String type, final SuccessListener listener){
        ActivityService activityService = retrofit.create(ActivityService.class);
        Call call = activityService.follow(ac_id,uid,type);
        Callback<Success> callback = new Callback<Success>() {
            @Override
            public void onResponse(Call<Success> call, Response<Success> response) {
                if (response.isSuccessful() && response.body()!=null)
                {
                    listener.onResponse(response.body());
                }
            }

            @Override
            public void onFailure(Call<Success> call, Throwable t) {

            }
        };
        call.enqueue(callback);
    }

    public void isCollected(String ac_id, String uid, String type, final CollectedListener listener){
        ActivityService activityService = retrofit.create(ActivityService.class);
        Call call = activityService.isCollected(ac_id,uid,type);
        Callback<Collected> callback = new Callback<Collected>() {
            @Override
            public void onResponse(Call<Collected> call, Response<Collected> response) {
                if (response.isSuccessful() && response.body()!=null)
                {
                    listener.onResponse(response.body());
                }
            }

            @Override
            public void onFailure(Call<Collected> call, Throwable t) {

            }
        };
        call.enqueue(callback);
    }

    public void buy(String ordername,String price,String num,String userid,final SuccessListener listener){
        ActivityService activityService = retrofit.create(ActivityService.class);
        Call call = activityService.buy(ordername,price,num,userid);
        Callback<Success> callback = new Callback<Success>() {
            @Override
            public void onResponse(Call<Success> call, Response<Success> response) {
                if (response.isSuccessful() && response.body()!=null)
                {
                    listener.onResponse(response.body());
                }
            }

            @Override
            public void onFailure(Call<Success> call, Throwable t) {

            }
        };
        call.enqueue(callback);
    }

    public void getOrder(String userid,String ordernum,final OrderListener listener){
        ActivityService activityService = retrofit.create(ActivityService.class);
        Call call = activityService.getOrder(userid,ordernum);
        Callback<Order> callback = new Callback<Order>() {
            @Override
            public void onResponse(Call<Order> call, Response<Order> response) {
                if (response.isSuccessful() && response.body()!=null)
                {
                    listener.onResponse(response.body());
                }
            }

            @Override
            public void onFailure(Call<Order> call, Throwable t) {

            }
        };
        call.enqueue(callback);
    }
}
